package com.wokeMy.view.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wokeMy.view.Lottery.OpenLottActivity;
import com.wokeMy.view.interfa.MyItemClickListener;
import com.wokeMy.view.model.DoubleLo;
import com.wokeMy.view.util.FullyLinearLayoutManager;
import com.wokeMy.view.util.SpaceItemDecoration;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import java.util.ArrayList;


/**
 * Created by Administrator on 2017-11-15.
 */

public class GGaoLotterAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;

    ArrayList<DoubleLo> data;

    public GGaoLotterAdapter(Context context, ArrayList<DoubleLo> data) {
        this.inflater=LayoutInflater.from(context);
        this.context=context;
        this.data=data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold hold=null;
        if(convertView==null){
            hold=new ViewHold();
            convertView=inflater.inflate(R.layout.content_lotteryinfor_lv,null);
            hold.typeTv=(TextView)convertView.findViewById(R.id.type_content_tv);
            hold.qiShuTv=(TextView)convertView.findViewById(R.id.qishu_content_tv);
            hold.DateTv=(TextView)convertView.findViewById(R.id.date_content_tv);
            hold.ballLv=(RecyclerView)convertView.findViewById(R.id.recyclerHorizontalView);
            hold.yinkSum=(TextView)convertView.findViewById(R.id.yingk_sum_tv);
            hold.ballLv.addItemDecoration(new SpaceItemDecoration(10));
            convertView.setTag(hold);
        }else{
            hold=(ViewHold) convertView.getTag();
        }

        FullyLinearLayoutManager horizontalLayoutManager = new FullyLinearLayoutManager(context);
        horizontalLayoutManager.setOrientation(FullyLinearLayoutManager.HORIZONTAL);
        hold.ballLv.setLayoutManager(horizontalLayoutManager);

        final DoubleLo doubleLo = data.get(position);
        ArrayList<String> strings = Util.dealtIssueGg(data.get(position).getBonuscode());
        final GGaoAdapter adapter = new GGaoAdapter(strings,context,doubleLo.getLotteryid());
        hold.ballLv.setAdapter(adapter);
        //调用方法,传入一个接口回调
        adapter.setItemClickListener(new MyItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                Intent intent= new Intent();
                intent.setClass(context, OpenLottActivity.class);
                intent.putExtra("lotteryid",doubleLo.getLotteryid());
                context.startActivity(intent);
            }
        });
        int sum=0;
        for (int i = 0; i < strings.size(); i++) {
            sum=Integer.valueOf(strings.get(i))+sum;
        }
        switch (data.get(position).getLotteryid()) {
            case "123":
                hold.yinkSum.setVisibility(View.VISIBLE);
                hold.yinkSum.setText("和值："+sum);
                break;
            default:
                hold.yinkSum.setVisibility(View.GONE);
                break;
        }
        hold.typeTv.setText(data.get(position).getTitle());
        hold.qiShuTv.setText("第"+data.get(position).getIssue()+"期");
        hold.DateTv.setText(data.get(position).getStarttime().split(" ")[0]);
        return convertView;
    }

    class ViewHold {
        TextView typeTv;
        TextView qiShuTv;
        TextView DateTv;
        TextView yinkSum;
        RecyclerView ballLv;
    }

}
